<template>
	<view class="container">
			
			
			<view class="uni-form-item uni-column">
							
							账号：<input class="uni-input" type="text" v-model="identity_card" placeholder="请输入账号(住院号 )" focus maxlength="18"  />
						</view>
						<view class="title">&nbsp;</view>
						<view class="uni-form-item uni-column">
										
						密码： <!-- #ifdef APP -->
						
						<!-- #endif --><input class="uni-input" type="password" v-model="password" placeholder="请输入密码" />
						</view>
		<button class="submit-button" @click="login">登录</button>
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				
					identity_card: '',
					password: ''
				
				
			}
		},
		mounted() {
				// 获取存储的数据
				let identity_card = uni.getStorageSync("identity_card");
				let userinfo = uni.getStorageSync("userinfo");
				let user_id = uni.getStorageSync("user_id");

				// 检查所有数据项是否存在
				if (userinfo && user_id) {
					// 如果三个数据项都存在，执行跳转
					uni.navigateTo({
						url: '/pages/index/tabbar?role=user'
					});
				} else {
					// 如果有数据不存在，可以选择不做任何事，或者执行其他逻辑，如跳转到登录页面等
					console.log("Some data is missing, cannot proceed to the homepage.");
					// 可以选择跳转到登录页面或者显示提示
					// uni.navigateTo({
					//     url: '/pages/login/login'
					// });
				}

			},
		methods: {
				login() {
						let formData = {
							identity_card: this.identity_card,
							password: this.password
						};
						console.log(formData);
			
						if (formData.identity_card === '' || formData.password === '') {
							uni.showToast({
								title: '请输入账号和密码',
								icon: 'none'
							});
						} else {
							// 使用uni.request 发送post请求
							uni.request({
								url: 'http://202.127.200.31:30010/auth/sqltestuser_login',
								method: 'POST',
								data: formData,
								success: (res) => {
									console.log('提交成功:', res.data);
									if (res.data.code == 20000) {
										uni.showToast({
											title: '登录成功',
											icon: 'success'
										});
										uni.setStorageSync("identity_card", this.identity_card);
										uni.setStorageSync("user_id", res.data.data.userid);
										uni.setStorageSync("userinfo", res.data);
										if (res.data.role === 'user') {
											setTimeout(() => {
												uni.redirectTo({
													url: '/pages/index/tabbar?role=user'
												});
											}, 500);
										} else if (res.data.role === 'doctor') {
											setTimeout(() => {
												uni.redirectTo({
													url: '/pages/index/tabbar?role=doctor'
												});
											}, 500);
										}
									} else {
										uni.showToast({
											title: '登录失败: ' + res.data.message,
											icon: 'none'
										});
									}
								},
								fail: (err) => {
									console.error('请求失败:', err);
									uni.showToast({
										title: '请求错误',
										icon: 'none'
									});
								}
							});
							}
							}
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
		background: linear-gradient(135deg, #71b7e6, #9b59b6);
	}

	.title {
		font-size: 28px;
		font-weight: bold;
		text-align: center;
		margin-bottom: 30px;
		color: #fff;
		text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
	}

	.form-item {
		display: flex;
		flex-direction: column;
		width: 100%;
		max-width: 300px;
		margin-bottom: 20px;
	}

	.form-item text {
		font-size: 16px;
		margin-bottom: 5px;
		color: #fff;
	}

    .uni-input-wrapper {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        padding: 8px 13px;
        flex-direction: row;
        flex-wrap: nowrap;
        background-color: #FFFFFF;
    }

    .uni-input {
        height: 28px;
        line-height: 28px;
        font-size: 15px;
        padding: 0px;
     
        background-color: #FFFFFF;
    }

	.submit-button {
		width: 100%;
		max-width: 300px;
		padding: 15px;
		background-color: #007aff;
		color: white;
		border: none;
		border-radius: 5px;
		font-size: 18px;
		text-align: center;
		margin-top: 20px;
		cursor: pointer;
		transition: all 0.3s ease;
	}

	.submit-button:hover {
		background-color: #005bb5;
		box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
	}

	.submit-button:active {
		transform: scale(0.98);
	}
</style>